﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using SimpleNoteLib.Base64;
using SimpleNoteLib.HttpRequests;

namespace SimpleNoteLib.ApiRequests
{
    internal sealed class LoginRequest : HttpPostRequest
    {
        #region Fields

        private string email;
        private string password;

        #endregion

        #region Properties

        public string Email
        {
            get { return email; }
            set
            {
                email = value;
                EncodeData();
            }
        }

        //TODO Check for secure string
        public string Password
        {
            get { return password; }
            set
            {
                password = value;
                EncodeData();
            }
        }

        #endregion

        #region Constructors

        public LoginRequest()
        {
        }

        public LoginRequest(string address, IWebProxy proxy = null)
            : base(address, proxy)
        {
        }

        public LoginRequest(string address, string email, string password, IWebProxy proxy = null)
            : base(address, proxy)
        {
            Email = email;
            Password = password;
        }

        public LoginRequest(string address, byte[] data, IWebProxy proxy = null)
            : base(address, data, proxy)
        {
        }

        public LoginRequest(string address, byte[] data, string email, string password, IWebProxy proxy = null)
            : base(address, data, proxy)
        {
            Email = email;
            Password = password;
        }

        public LoginRequest(string email, string password)
        {
            Email = email;
            Password = password;
        }

        private void EncodeData()
        {
            string data = string.Format("email={0}&password={1}", Email, Password);
            string base64Data = Base64Coder.EncodeBase64(data);
            Data = Encoding.UTF8.GetBytes(base64Data);
        }

        public override string SendAndGetResponse()
        {
            EncodeData();
            return base.SendAndGetResponse();
        }

        #endregion
    }
}
